<?php
include("_config.php");
class Mgame extends CI_Model {

    

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function isPlaying($hash)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT isPlaying FROM users WHERE hash = :hash");
        $req->execute(array('hash' => $hash)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        $req->closeCursor();
        if($result == 1)
        {
            return true;
        } else {
            return false;
        }
    }
    
    function getRoomInfo($room_id)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT * FROM users WHERE room = :room");
        $req->execute(array('room' => $room_id)) or die(print_r($req->errorInfo()));
        $result = $req->fetchAll();
        $req->closeCursor();
        if($result){
            return $result;
        } else {
            return false;
        }
    }
    
    function getHashUsersOnline()
    {
        $PDO = connectDatabase();
        $req = $PDO->query("SELECT hash FROM users");
        $res = $req->fetchAll();
        $req->closeCursor();
        if($res)
        { return $res; }
        else { return null; }
    }
    
    function createRooms($hash1, $hash2)
    {
        $PDO = connectDatabase();
        $continue = true;
        while($continue == true){
            $room_id = rand(); 
            $req = $PDO->prepare("SELECT ID FROM users WHERE room = :room");
            $req->execute(array('room' => $room_id)) or die(print_r($req->errorInfo()));
            $result = $req->fetch();
            $req->closeCursor();
            if(!$result)
            {
                $continue = false;
            }
        }
        $heure = time() + 60;
        $req = $PDO->prepare("UPDATE users SET expire = :heure, isPlaying = 1, room = :room WHERE hash = :hash1 OR hash = :hash2");
        $req->execute(array('hash1' => $hash1, 'hash2' => $hash2, "heure" => $heure, 'room' => $room_id)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
        return $room_id;
    }
    
    function finishGame($room_id)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("UPDATE users SET isPlaying = 0, room = null WHERE room = :room");
        $req->execute(array('room' => $room_id)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
        return $room_id;
    }
    
    function modifyScore($pseudo, $win, $loose)
    {
        $PDO = connectDatabase();
        $req = $PDO->prepare("SELECT win, lost FROM membres WHERE login = :pseudo");
        $req->execute(array('pseudo' => $pseudo)) or die(print_r($req->errorInfo()));
        $result = $req->fetch();
        echo $pseudo." "; print_r($result);
        $req->closeCursor();
        if($win)
        {
            $result["win"]++;
        }
        if($loose)
        {
            $result["lost"]++;
        }
        $req = $PDO->prepare("UPDATE membres SET win = :win, lost = :loose WHERE login = :pseudo");
        $req->execute(array('win' => $result["win"], 'loose' => $result["lost"], 'pseudo' => $pseudo)) or die(print_r($req->errorInfo()));
        $req->closeCursor();
        return true;
    }
    
    
}